package com.zx.article.articleType.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zx.article.articleType.dao.IArticleTypeDao;
import com.zx.article.articleType.model.ArticleTypeModel;
import com.zx.article.util.DBUtils;

public class ArticleTypeDaoImpl implements IArticleTypeDao {

	private DBUtils dbUtils = new DBUtils();

	@Override
	public String insert(String name) {
		String sqlString = "INSERT INTO article_type(article_type.at_name) VALUES(?)";
		Object[] valObjects = { name };
		return dbUtils.update(sqlString, valObjects);
	}

	@Override
	public List<ArticleTypeModel> queryList(ArticleTypeModel articleTypeModel) {
		StringBuffer sqlBuffer = new StringBuffer("SELECT at_id,at_name FROM article_type ");
		List<Object> appendWhere = appendWhere(sqlBuffer, articleTypeModel);
		return dbUtils.queryList(sqlBuffer.toString(), appendWhere.toArray(), ArticleTypeModel.class, getMap());
	}

	private List<Object> appendWhere(StringBuffer sqlBuffer, ArticleTypeModel articleTypeModel) {
		List<Object> list = new ArrayList<>();
		sqlBuffer.append("where 1=1 ");
		String name = articleTypeModel.getName();
		if (name != null && !name.isEmpty()) {
			sqlBuffer.append("and at_name like ? ");
			list.add(name);
		}
		if (articleTypeModel.isPageOn()) {
			sqlBuffer.append("limit ?,? ");
			list.add(articleTypeModel.getRowStart());
			list.add(articleTypeModel.getRowCount());
		}
		return list;
	}

	private Map<String, String> getMap() {
		Map<String, String> map = new HashMap<>();
		map.put("id", "at_id");
		map.put("name", "at_name");
		return map;
	}

	@Override
	public int queryCount(ArticleTypeModel articleTypeModel) {
		StringBuffer sqlBuffer = new StringBuffer("SELECT COUNT(1) FROM article_type ");
		List<Object> appendWhere = appendWhere(sqlBuffer, articleTypeModel);
		return dbUtils.queryCount(sqlBuffer.toString(), appendWhere.toArray());
	}

	@Override
	public String delete(String name) {
		String sqlString = "DELETE FROM article_type WHERE at_name = ? ";
		Object[] valObjects = { name };
		return dbUtils.update(sqlString, valObjects);
	}

	@Override
	public ArticleTypeModel getModel(String name) {
		String sqlString = "SELECT at_id,at_name FROM article_type WHERE at_name = ?";
		Object[] valObjects = { name };
		return dbUtils.queryOne(sqlString, valObjects, ArticleTypeModel.class, getMap());
	}

	@Override
	public String updata(ArticleTypeModel articleTypeModel) {
		String sqlString = "UPDATE article_type SET at_name = ? WHERE at_id = ?";
		Object[] valObjects = { articleTypeModel.getName(), articleTypeModel.getId() };
		return dbUtils.update(sqlString, valObjects);
	}

	@Override
	public int queryArticleCount(String name) {
		String sqlString = "SELECT COUNT(1) FROM article WHERE a_type_id IN ( SELECT at_id FROM article_type WHERE article_type.at_name = ?)";
		Object[] valObjects = { name };
		return dbUtils.queryCount(sqlString, valObjects);
	}

}
